package com.yq.core.util;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class LoadImg {
    public void makeImage(String urlString, String savePath) {

        try {
            URL url = new URL(urlString);
            // 打开连接
            URLConnection con = url.openConnection();
            //设置请求的路径
            con.setConnectTimeout(5 * 1000);
            // 输入流
            InputStream is = con.getInputStream();

            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;

            OutputStream os = new FileOutputStream(savePath);
            // 开始读取
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            // 完毕，关闭所有链接
            os.close();

            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        LoadImg li = new LoadImg();
        for (int i = 0; i < 500; i++) {
            String number = "";
            if (i < 10) {
                number = "00" + i;
            } else if (i < 100 && i >= 10) {
                number = "0" + i;
            } else {
                number = i+"";
            }

            String name = "star_" + number + "_pc_x2.png";

            li.makeImage(
                    "http://img.t.sinajs.cn/t6/skin/public/feed_cover/" + name,
                    "C:\\Users\\Administrator\\Desktop\\loadimg\\" + name
            );
        }

    }

}
